/**
 * Created by 修 on 2015/2/14.
 */
var user="修";
var base_url = "index.php/";
$(function(){
    load_article();
    edit_new();
    search();
});
function load_article(condition){
    $(".content").empty();
    $.post(base_url+"article/all",condition?{condition:condition}:{},function(article){
        $.each(article,function(i){
            new card({
                time : article[i].time,
                content:article[i].content,
                comment:(article[i].comment||"").split("<>"),
                good:article[i].good,
                bad:article[i].bad,
                aid:article[i].aid
            });
        })
    },"json")
}
function edit_new(){
    $(".add").click(function(){
        $(this).toggleClass("add_active");
        $(".add_mask").fadeToggle("fast");
    });
    add_send();
}
function add_send(){
    $(".add_send").click(function(){
        $(this).addClass("add_send_active").html("发送中……");
        var text = $("textarea[name='add_article']").val();
        $.ajax({
            url:base_url+"article/add",
            data:{content:text},
            type:"POST",
            success:function(){
                $(".add_send").eq(0).html("发送").removeClass("add_send_active");
                $(".add").trigger("click");
                load_article();
            },
            error:function(){

            }
        });
    });
}
function search(){
    $(".icon-search").click(function(){
        load_article($("input[name='search']").val());
    });
    $("input[name='search']").on("keypress",function(e){
        if(e.keyCode==13)  load_article($(this).val());
    });
}